<template>
	<view class='page_cont'>
		<view class="top_wrap" v-if="type==1">
			<view class='item' :class="check_index==index?'active':''" v-for="(item,index) in checks" :key="index" @click="check_cont(index)">
				<view class='text'>{{item}}</view>
				<view class='line' v-if="check_index==index"></view>
			</view>
		</view>
		<view class='item_cont' style="padding-top:114rpx;" v-if="type==1">
			<view class='job_cont' v-if="check_index==0">
				<jobItem :jobs="jobs" :status="5"></jobItem>
			</view>
			<view class='mech_cont' v-if="check_index==1">
				<mechItem :mechs="mechs" :status="4"></mechItem>
			</view>
		</view>
		<view class='item_cont' v-if="type==2">
			<view class='teach_cont'>
				<teacherItem :teachers="teachers" :status="5"></teacherItem>
			</view>
		</view>
		<view class='no_cont' v-if="show_nav">
			<view class="no_box">
				<view class='title'>暂时还没有收藏记录哦</view>
				<view class='text'>积极的人生，才有更好的收获！</view>
			</view>
			<view class='nav_btn' @click="nav_home()">去首页</view>
		</view>
	</view>
</template>

<script>
	import home from "../../api/home.js"
	import JobItem from "../../components/jobItem.vue"
	import MechItem from "../../components/mechItem.vue"
	import TeacherItem from "../../components/teacherItem.vue"
	import util from "../../utils/util.js"
	export default{
		data(){
			return{
				type: '',
				checks: ["职位","单位"],
				check_index: 0,
				jobs: [],
				mechs: [],
				teachers: [],
				show_nav: false,
				currentPage: 1,
				pageSize: 10,
				latitude: 22.820794,
				longitude: 113.284565,
				hasMoreData: true,
				refresh: false,
				user_address: ''
			}
		},
		onLoad(options){
			var that=this;
			that.type=options.type
			if(options.type==1){
				that.get_job_colls()
			}else{
				that.get_resume_colls()
			}
			this.user_address=uni.getStorageSync("user_address")
		},
		onShow() {
			// console.log(this.refresh)
			if(this.refresh){
				this.refresh=false
				this.currentPage=1
				if(this.type==1){
					if(this.check_index==0){
						this.jobs=[]
						this.get_job_colls()
					}else{
						this.mechs=[]
						this.get_mech_colls()
					}
				}else{
					this.teachers=[]
					this.get_resume_colls()
				}
			}
		},
		// 触底
		onReachBottom() {
			var that=this;
			if(that.hasMoreData){
				if(that.type==1){
					if(that.check_index==0){
						that.get_job_colls()
					}else{
						that.get_mech_colls()
					}
				}else{
					that.get_resume_colls()
				}
			}else{
				util.page_show_toast('没有更多了',1500)
			}
		},
		methods:{
			// 选择(老师)
			check_cont(index){
				if(this.check_index!=index){
					this.check_index=index;
					this.currentPage=1;
					this.show_nav=false;
					if(index==0){
						this.jobs=[]
						this.get_job_colls()
					}else{
						this.mechs=[]
						this.get_mech_colls()
					}
				}
			},
			// 回首页
			nav_home(){
				uni.switchTab({
					url: '../../pages/index/index'
				})
			},
			// 职位收藏列表
			get_job_colls(){
				uni.showLoading({})
				var params={
					"targetType": 1,
					"currentPage": this.currentPage,
					"pageSize": this.pageSize,
					"sidx": "create_time",
					"order": "desc",
					"lat": this.user_address?this.user_address.lat:this.latitude,
					"lng": this.user_address?this.user_address.lng:this.longitude
				}
				home.get_job_colls(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
						item.salary = item.salaryMin==0?"面议":`${item.salaryMin/1000}-${item.salaryMax/1000}k/月`
					})
					//
					var jobs=util.extend(this.jobs, list)
					if (list.length < this.pageSize) {
						this.jobs=jobs;
						this.hasMoreData=false;
					} else {
						this.jobs=jobs;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&jobs.length==0){
						this.show_nav=true;
					}
				})
			},
			// 机构收藏列表
			get_mech_colls(){
				uni.showLoading({})
				var params={
					"targetType": 3,
					"currentPage": this.currentPage,
					"pageSize": this.pageSize,
					"lat": this.user_address?this.user_address.lat:this.latitude,
					"lng": this.user_address?this.user_address.lng:this.longitude
				}
				home.get_mech_colls(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.space=item.distance>1000?(item.distance/1000).toFixed(2):item.distance;
					})
					//
					var mechs=util.extend(this.mechs, list)
					if (list.length < this.pageSize) {
						this.mechs=mechs;
						this.hasMoreData=false;
					} else {
						this.mechs=mechs;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&mechs.length==0){
						this.show_nav=true;
					}
				})
			},
			// 简历收藏列表
			get_resume_colls(){
				uni.showLoading({})
				var params={
					"targetType": 2,
					"sidx": "create_time",
					"order": "desc",
					"pageSize": this.pageSize,
					"currentPage": this.currentPage
				}
				home.get_resume_colls(params).then(res=>{
					uni.hideLoading();
					var list=res.data.data.records;
					list.forEach((item)=>{
						item.age=new Date().getFullYear()-new Date(item.birthday).getFullYear();
						item.workYear=new Date().getFullYear()-new Date(item.joinWorkTime).getFullYear();
						item.salary = item.salaryMin==0?"面议":`${item.salaryMin/1000}-${item.salaryMax/1000}k/月`
						item.works.forEach((itm)=>{
							itm.start=util.formatTime1(new Date(itm.entryTime),1);
							itm.end=util.formatTime1(new Date(itm.quitTime),1)
						})
						if(item.works.length>0){
							item.maxIndex=util.getMaxIndex(item.works)
						}
					})
					//
					var teachers=util.extend(this.teachers, list)
					if (list.length < this.pageSize) {
						this.teachers=teachers;
						this.hasMoreData=false;
					} else {
						this.teachers=teachers;
						this.hasMoreData=true;
						this.currentPage=this.currentPage+1;
					}
					if(list.length==0&&teachers.length==0){
						this.show_nav=true;
					}
				})
			},
		},
		components: {
			jobItem: JobItem,
			mechItem: MechItem,
			teacherItem: TeacherItem
		},
	}
</script>

<style lang="scss">
	page{
		background: #F6F7F9;
		.page_cont{
			width: 100%;
			.top_wrap{
				position: fixed;
				/* #ifdef MP-WEIXIN */
				top: 0;
				/* #endif */
				/* #ifdef H5 */
				top: 44px;
				/* #endif */
				left: 0;
				width: 100%;
				box-sizing: border-box;
				background: #fff;
				padding: 32rpx 188rpx 8rpx 188rpx;
				display: flex;
				flex-direction: row;
				z-index: 990;
				.item{
					width: 72rpx;
					font-weight: 400;
					color: #929BA7;
					&:first-child{
						margin-right: 230rpx;
					}
					.text{
						width: 72rpx;
						height: 50rpx;
						font-size: 36rpx;
						line-height: 50rpx;
						margin-bottom: 4rpx;
					}
					.line{
						width: 52rpx;
						margin: auto;
						height: 8rpx;
						background: #5B90FF;
						border-radius: 2rpx;
					}
				}
				.active{
					color: #3F4A58;
					font-weight: 600;
				}
			}
			.item_cont{
				width: 100%;
			}
			.no_cont{
				width: 520rpx;
				margin: 110rpx auto 0 auto;
				.no_box{
					width: 100%;
					height: 520rpx;
					box-sizing: border-box;
					padding-top: 386rpx;
					background: url("https://ossdev0.jiabala.com/7XyPjGSZsyaek") no-repeat 0 0/100% 100%;
					.title{
						text-align: center;
						height: 50rpx;
						font-size: 32rpx;
						font-weight:400;
						line-height: 50rpx;
						color: #3F4A58;
					}
					.text{
						margin-top: 8rpx;
						text-align: center;
						height: 40rpx;
						font-size: 28rpx;
						font-weight:400;
						line-height: 40rpx;
						color: #798594;
					}
				}
				.nav_btn{
					width: 272rpx;
					margin: auto;
					height: 88rpx;
					background: #5B90FF;
					text-align: center;
					font-size: 30rpx;
					font-weight: 400;
					line-height: 88rpx;
					color:#fff;
					letter-spacing:2rpx;
					border-radius: 16rpx;
				}
			}
		}
	}
</style>
